<template>
  <div class="login_container">
    <el-card class="login_box">
      <div slot="header" class="clearfix">
        <el-image
          style="width: 100px; height: 100px"
          :src="require('@/assets/logo.png')"
        ></el-image>
      </div>
      <el-form
        ref="ruleform"
        :rules="rules"
        :model="userinfo"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            prefix-icon="iconfont icon-wode"
            v-model="userinfo.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            prefix-icon="iconfont icon-yanzhengma"
            v-model="userinfo.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btnBox">
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button @click="clearFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { UserInfoRules } from '@/common/mixin'
import { getUserInfoApi } from '@/Utils/Api/getUserInfoApi'
export default {
  name: 'LoginView',
  mixins: [UserInfoRules],
  data () {
    return {
      url: "require('@/assets/logo.png')",
      userinfo: {
        username: 'admin',
        password: '123456'
      }
    }
  },
  components: {},
  created () {},
  mounted () {},
  methods: {
    onSubmit () {
      console.log('点击这里是登录')
      this.$refs.ruleform.validate(async (res) => {
        console.log('表单验证通过，发起登录请求')
        console.log(res)
        const { data } = await getUserInfoApi(this.userinfo)
        console.log(data)
        if (data.meta.status !== 200) {
          return this.$message.warning(data.meta.msg)
          // return console.log('登录失败' + data.meta.msg)
        } else {
          this.$message({
            type: 'success',
            message: data.meta.msg,
            duration: 750
          },
          window.localStorage.setItem('token', data.data.token),
          this.$router.push('/home')
          )
        }
      })
    },
    clearFrom () {
      this.$refs.ruleform.resetFields()
      this.userinfo = {
        username: '',
        password: ''
      }
    }
  },
  computed: {},
  watch: {}
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: rgb(149, 154, 232);
  height: 100vh;
  /deep/.login_box {
    width: 450px;
    height: 350px;
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    box-shadow: 0 0 20px #ddd;
    .el-card__header {
      height: 125px;
      .el-image {
        box-shadow: 0 0 10px #ddd;
        border-radius: 50%;
        transform: translate(0px, -7px);
        .el-image__inner {
          width: 75px;
          height: 75px;
          border-radius: 45%;
          transform: translate(0px, 12px);
        }
      }
    }
    .btnBox {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
